﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Common;
using Enter.DataProxy.Cache.Configuration;
using Enter.Common.Helper;

namespace Enter.DataProxy.Cache
{
    public class CacheService
    {
        // Fields
        private static ICacheService current;
        private static object lockObject;

        // Methods
        static CacheService()
        {
            current = null;
            lockObject = new object();
        }


        // Properties
        public static ICacheService Current
        {
            get
            {
                if (!EntlibCache.CheckCacheConfig())
                {
                    lock (lockObject)
                    {
                        if (current != null)
                        {
                            //current.ReleaseConnection();
                            current = null;
                        }
                    }
                }
                if (current == null)
                {
                    lock (lockObject)
                    {
                        if (current == null)
                        {
                            try
                            {
                                //INoSqlCacheService noSqlCacheService = new RedisCacheService();
                                CacheType Cachetype = new CacheConfigurationManager().GetCacheSettings().Cachetype;
                                switch (Cachetype)
                                {
                                    case CacheType.webcache:
                                        current = new WebCache();
                                        break;
                                    case CacheType.memcache:
                                        current = new MemCache(true);
                                        break;
                                    case CacheType.nosql:
                                       // current = new NoSqlCache(noSqlCacheService);
                                        break;
                                    default:
                                        //current = new FabricCache();
                                        break;
                                }
                            }
                            catch (Exception e)
                            {
                                Error(e);
                            }
                        }
                    }
                }

                return current;
            }
        }

        /// <summary>
        /// 缓存服务器出错,切换到无缓存状态!
        /// </summary>
        public static void Error(Exception e)
        {
            lock (lockObject)
            {
                //记录异常日志 并发送邮件
                LogHelper.Log(e);
                //发送邮件 缓存集群已经挂了
                //current.ReleaseConnection();
                current = new ErrorCache();
            }
        }


        /// <summary>
        /// 重置缓存,缓存服务器恢复切换到设置缓存状态
        /// </summary>
        public static void Reset()
        {
            lock (lockObject)
            {
                current.ReleaseConnection();
                current = null;
            }
        }


    }
}
